package com.wawayaya.bookreader.worddict;

import android.content.Context;
import android.graphics.Paint.FontMetrics;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;

public class ViewWordDictText extends TextView {

	private int m_iminwidth = 0;
	private int m_imaxwidth = 0;
	private int m_iviewwidth = 0;
	private int m_iviewheight = 0;
	private StringBuilder m_sbtext = new StringBuilder();
	
	public ViewWordDictText(Context context) {
		super(context);
	}
	public ViewWordDictText(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
        public ViewWordDictText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	
	public void setviewwidthrestriction(int iminwidth, int imaxwidth) {
		m_iminwidth = iminwidth;
		m_imaxwidth = imaxwidth;
	}
	
	public void setbasewordtext(String stext, boolean bsettext){
		m_sbtext.delete(0, m_sbtext.length());
		m_sbtext.append(stext);
		TextPaint p = getPaint();
		//float f = p.getTextSize();
		calculatebasewordtextsize(p, stext);
		if(bsettext) {
			setText(stext);
		}
	}
	
	public void setindicatedtext(String stext, boolean bsettext) {
		m_sbtext.delete(0, m_sbtext.length());
		m_sbtext.append(stext);
		TextPaint p = getPaint();
		//float f = p.getTextSize();
		calculatetextsize(p, stext);
		if(bsettext) {
			setText(stext);
		}
	}
	
	public int getviewwidth() {
		return m_iviewwidth;
	}
	
	public int getviewHeight() {
		return m_iviewheight;
	}

	public String getviewText() {
		return m_sbtext.toString();
	}
	
	protected void calculatebasewordtextsize(TextPaint ptp, String stext) {
		if(null == ptp) {
			return;
		}
		if(TextUtils.isEmpty(stext)){
			stext = " ";
		}
		FontMetrics fm = ptp.getFontMetrics();
		int ionelineheight = (int)(fm.bottom - fm.top + 0.499f);
		m_iviewwidth = (int)(ptp.measureText(stext) + 0.499f);
		if(m_iviewwidth >= m_imaxwidth){
			m_iviewwidth = m_imaxwidth;
			m_iviewheight = ionelineheight * gettextlines(ptp, stext, m_imaxwidth);
		}
		else{
			m_iviewwidth += 10;
			m_iviewheight = ionelineheight;
		}
	}
	
	protected void calculatetextsize(TextPaint ptp, String stext) {
		if(null == ptp) {
			return;
		}
		if(TextUtils.isEmpty(stext)){
			stext = " ";
		}
		FontMetrics fm = ptp.getFontMetrics();
		int ionelineheight = (int)(fm.bottom - fm.top + 0.5f);
		m_iviewwidth = (int)(ptp.measureText(stext) + 0.499f);
		if(m_iviewwidth >= m_imaxwidth){
			m_iviewwidth = m_imaxwidth;
			m_iviewheight = ionelineheight * gettextlines(ptp, stext, m_imaxwidth);
		}
		else{
			m_iviewwidth = Math.max(m_iviewwidth, m_iminwidth);
			m_iviewheight = ionelineheight;
		}
	}
	
	private int gettextlines(TextPaint p, String stext, int ilimitwidth) {
		int ilines = 1;
		float fwidth = 0f;
		if(null != p && !TextUtils.isEmpty(stext)) {
			int iextra = (int)(p.measureText('\n' + "") + 0.49f);
			int istartpos = 0;
			for(int i = 0; i < stext.length(); i++) {
				char c = stext.charAt(i);
				fwidth += p.measureText(c + "");
				if(fwidth - (m_imaxwidth + iextra) > 0.01f) {
					i = adjustnextbreaklinestartpos(stext, istartpos, i - 1);
					m_sbtext.insert(i + (ilines - 1), '\n');
					istartpos = i + 1;
					fwidth = 0;
					ilines++;
				}
			}
		}
		return ilines;
	}
	
	private int adjustnextbreaklinestartpos(String stext, int istartpos, int iendpos) {
		if(iendpos < 0 || iendpos <= istartpos) {
			return iendpos + 1;
		}
		try{
			int i = iendpos;
			for(; i > istartpos; i--) {
				if(characterisabletoaslineend(stext.codePointAt(i))) {
					return i;
				}
			}
			if(i == istartpos) {
				i = iendpos;
			}
		}catch(Exception e){
		}
		return iendpos;
	}	
	
	private boolean characterisabletoaslineend(int ichara) {
		if(ichara > 0x80 && '“' != ichara && '‘' != ichara && '《' != ichara && '（' != ichara && '：' != ichara) {
			return true;
		}
		if(' ' == ichara || ',' == ichara || '.' == ichara || '?' == ichara || ';' == ichara || '!' == ichara || ')' == ichara){
			return true;
		}
		return false;
	}
	
}
